import java.io.*;
import java.nio.file.Files;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-05
 * Time: 16:10
 */
public class IODemo16 {
    //复制一个源文件
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要复制的源文件的所在路径");
        String scrPath = scanner.next();
        System.out.println("请输入复制后的文件目标所在的路径");
        String desPath = scanner.next();

        //1：判断源文件是否存在
        File scrFile = new File(scrPath);
        if (!scrFile.exists()){
            System.out.println("你要找的源文件不存在");
        }
        /*
        * 目标文件的父目录存在即可*/
        File desFile = new File(desPath);
        if (!desFile.getParentFile().exists()){
            System.out.println("目标路径有误");
        }

        /*
        * 分别以读、写的方式打开文件*/
        try(InputStream inputStream = Files.newInputStream(scrFile.toPath());
            OutputStream outputStream = new FileOutputStream(desFile)){
            while(true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (n == -1){
                    break;
                }
                outputStream.write(buffer,0,n);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
